<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="./vue.js"></script>
<script src="./vuex.js"></script>

<body>
  <div id="app">
    <div class="count">
      <button @click="incre">计数</button>
      <span>{{ this.$store.state.count}}</span>
      <button @click="decre">减数</button>
    </div>
  </div>

  <script>
    const store = new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment(state) {
          state.count++;
        },
        decrement(state) {
          state.count--;
        }
      },
      actions: {
        add(context) {
          context.commit("increment");
        },
        reduce(context) {
          context.commit("decrement");
        }
      }
    })

    const vm = new Vue({
      el: "#app",
      store,
      methods: {
        incre() {
          this.$store.dispatch("add")
        },
        decre() {
          this.$store.dispatch("reduce")
        }
      }
    })
  </script>
</body>

</html>